<template>
  <div class="product">
    <nav-bar class="navBar">
      <span slot="left" class="iconfont icon-huitui leftInfo" @click="$router.back()"></span>
      <div slot="center" class="centerInfo">
        <input type="text" v-model="searchVal">
        <span class="search iconfont icon-sousuo"></span>
        <span class="cancel iconfont icon-quxiao" v-if="search" @click="clearSearchVal"></span>
      </div>
      <span slot="right" class="iconfont icon-gengduo right-icon"></span>
    </nav-bar>
    <ul class="sort">
      <li :class="{'active':type==0}" @click="headerProData(0)">默认排序</li>
      <li :class="{'active':type==1}" @click="headerProData(1)">
        <span>升序</span>
        <span class="iconfont icon-shengxu icon"></span>
      </li>
      <li :class="{'active':type==2}" @click="headerProData(2)">
        <span>降序</span>
        <span class="iconfont icon-jiangxu icon"></span>
      </li>
      <li>
        <span>筛选</span>
        <span class="iconfont icon-shaixuan icon"></span>
      </li>
    </ul>
    <scroll class="content" ref="content" @pullingUp="pullUpLoad" @pullingDown="pullDownLoad">
      <div class="list-box">
        <ul class="list" v-if="productList.length>0">
          <li v-for="item in productList" :key="item.id" @click="$router.push(`/detail/${item.id}`)">
            <img v-lazy="item.imageHost+item.mainImage" alt="">
            <div class="list-item">
              <p class="title">{{item.name}}</p>
              <p class="desc">{{item.subtitle}}</p>
              <p class="price">
                <span><i>￥</i>{{item.price}}</span>
                <span class="dele"><i>￥</i>{{item.originalPrice}}</span>
              </p>
            </div>
          </li>

        </ul>
      </div>
    </scroll>
  </div>
</template>
<script>
import navBar from 'components/common/navbar/navBar'
import Scroll from 'components/common/scroll/Scroll'
export default {
  name: '',
  data() {
    return {
      searchVal: "",
      type: 0,
      orderBy: '',
      id: this.$route.params.id,
      productList: [],
      pageNum: 1,
      pageSize: 10,
      length: 0,

    }
  },
  computed: {
    search() {
      return this.searchVal.trim() ? true : false
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.content.refresh()
    })
  },
  components: {
    navBar,
    Scroll
  },
  created() {
    this.headerProData(0)

  },
  methods: {

    // 下拉刷新
    pullDownLoad() {
      // 随机获取页面数据
      this.pageNum++
      this.length += this.pageSize
      if (this.pageNum > 3) {
        this.length = 0
        this.pageNum = 1
      }
      this.getProduct(3)
    },

    // 上拉加载
    pullUpLoad() {
      this.length += this.pageSize
      if (this.length > this.productList.length) {
        this.pageNum = 1
        return
      } else {
        this.pageNum++
        this.getProduct(2)
      }
    },
    async headerProData(val) {
      this.type = val
      if (val == 0) {
        this.orderBy = "default"
        this.length = 0
      } else if (val == 1) {
        this.orderBy = "price_asc"
        this.length = 0
      } else {
        this.orderBy = "price_desc"
        this.length = 0
      }
      this.getProduct(1)
    },

    async getProduct(val) {
      const res = await this.$require({
        url: '/product/list',
        method: 'get',
        params: {
          categoryId: this.id,
          orderBy: this.orderBy,
          pageNum: this.pageNum,
          pageSize: this.pageSize
        }
      })

      if (val == 1) {
        this.productList = res.data.list
        this.$refs.content.finishPullUp()
        this.$refs.content.finishPullDown()
        this.$refs.content.refresh()
        return
      }
      else if (val == 2) {
        this.productList.push(...res.data.list)
        //完成上拉加载更多
        this.$refs.content.finishPullUp()
        this.$refs.content.finishPullDown()
        this.$refs.content.refresh()
        return
      } else {
        this.productList = res.data.list
        this.length = 0
        // 完成下拉加载更多
        this.$refs.content.finishPullDown()
        this.$refs.content.finishPullUp()
        this.$refs.content.refresh()
        return
      }


    },

    clearSearchVal() {
      this.searchVal = ""
    }
  }
}
</script>
<style scoped lang="scss">
.product {
  width: 100%;
  height: 100%;
  background-color: #fff;
}
.navBar {
  box-sizing: border-box;
  overflow: hidden;
  position: fixed;
  z-index: 99;
  left: 0;
  top: 0;
  background-color: #f6f6f6;

  span {
    font-size: 14px;
  }
  .leftInfo {
    font-size: 20px;
    font-weight: 500;
    display: block;
  }
  .centerInfo {
    position: relative;
    input {
      width: 100%;
      height: 30px;
      border-radius: 20px;
      outline: none;
      border: 1px solid #eee;
      padding: 0px 35px 0px;
      box-sizing: border-box;
      font-size: 14px;
      line-height: 50px;
    }
    input::placeholder {
      color: #999;
      font-size: 13px;
    }
    .center-item {
      position: absolute;
      left: 0;
      top: 15px;
      color: rgb(246, 53, 21);
      font-size: 18px;
      font-weight: 600;
      display: inline-block;
      padding: 10px 6px 10px 10px;
      line-height: 0;
      border-right: 1px solid #999;
    }
    .search {
      position: absolute;
      left: 10px;
      color: #999;
      font-size: 16px;
      font-weight: bold;
    }
    .cancel {
      position: absolute;
      top: 0;
      right: 10px;
      color: #333;
      font-size: 16px;
      font-weight: 500;
    }
  }
  .right-icon {
    font-size: 20px;
    font-weight: 500;
    display: block;
  }
}
.sort {
  height: 50px;
  width: 100%;
  position: fixed;
  top: 50px;
  z-index: 99;

  display: flex;
  align-items: center;
  justify-content: space-between;
  text-align: center;

  border-top: 1px solid #dcdcdc;
  border-bottom: 1px solid #dcdcdc;
  background-color: #fff;
  padding: 0 20px;
  box-sizing: border-box;
  font-size: 14px;
  .icon {
    font-size: 12px;
    margin-left: 3px;
  }
}
.active {
  color: #f63515;
}

.content {
  position: absolute;
  top: 100px;
  left: 0;
  right: 0;
  bottom: 0;
}

.list {
  width: 100%;

  height: auto;
  li {
    width: 100%;
    height: 88px;
    background-color: #fafafa;
    padding: 0 10px;
    margin-top: 8px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-around;
    align-items: center;
    img {
      width: 88px;
      height: 88px;
      object-fit: cover;
    }
    .list-item {
      width: 260px;
      height: 88px;
      .desc {
        color: #999;
        font-size: 12px;
      }
      p {
        margin-top: 5px;
        padding-left: 5px;
        font-size: 13px;
        color: #333;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
        span {
          font-size: 14px;
          i {
            font-style: normal;
            font-size: 12px;
          }
        }
      }
      .title {
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }
      .price {
        margin-top: 10px;
      }
      .dele {
        font-size: 12px;
        color: #999;
        padding-left: 5px;
        text-decoration: line-through;
      }
    }
  }
}
</style>
